package org.canaan.http.core.post;

import org.apache.http.client.config.RequestConfig;
import org.canaan.http.HttpBuilder;
import org.canaan.http.core.AbstractHttpBuilder;
import org.canaan.http.core.single.RequestConfigFactory;
import org.canaan.http.type.ParamType;

import java.util.Map;

/**
 * @author Canaan
 * @date 2017/8/13
 */
public class PostHttpBuilder extends AbstractHttpBuilder {
    ParamType paramType;    //参数类型


    PostHttpBuilder() {
        super();
        paramType = ParamType.FORM_URLENCODED;
    }

    @Override
    public PostClient builder() {
        if (this.requestConfig != null) {
            return new PostClient(this);
        }

        if (this.hasCookies) {
            this.requestConfig = RequestConfigFactory.COOKIE_CONFIG;
        } else {
            this.requestConfig = RequestConfigFactory.DEFAULT_CONFIG;
        }
        return new PostClient(this);
    }

    public PostHttpBuilder setParamType(ParamType paramType) {
        this.paramType = paramType;
        return this;
    }

    /**
     是否自动关闭，默认是自动关闭
     @return
     */
    @Override
    public PostHttpBuilder notAutoClose() {
        this.autoClose = false;
        return this;
    }

    @Override
    public PostHttpBuilder hasCookies() {
        this.hasCookies = true;
        return this;
    }

    public PostHttpBuilder appendHeader(String key, String val) {
        this.header.put(key, val);
        return this;
    }

    @Override
    public PostHttpBuilder setHeader(Map<String, String> header) {
        this.header.clear();
        this.header.putAll(header);
        return this;
    }

    @Override
    public PostHttpBuilder setCookies(Map<String, Object> cookies) {
        this.cookies.clear();
        this.cookies.putAll(cookies);
        return this;
    }

    @Override
    public PostHttpBuilder appendCookie(String key, Object val) {
        this.cookies.put(key, val);
        return this;
    }

    @Override
    public PostHttpBuilder setRequestConfig(RequestConfig requestConfig) {
        this.requestConfig = requestConfig;
        return this;
    }
    @Override
    public PostHttpBuilder setRepeatTimes(Integer repeatTimes) {
        this.repeatTimes = repeatTimes;
        return this;
    }

    @Override
    public PostHttpBuilder isSingle(boolean isSingle) {
        this.isSingle = isSingle;
        return this;
    }

}
